/**
 *  @file Constrant.m
 *  @brief
 *
 *  Constrant.m
 *  iShow
 *
 *  Created by zhaohong on 16/3/11.
 *  Copyright © 2016年 zhaohong. All rights reserved.
 *
 */

#import "Constant.h"
#import "MBProgressHUD.h"
#import "BaseNaviController.h"
#import "sys/utsname.h"
#import <CommonCrypto/CommonCrypto.h>
//#import "SSKeychain.h"

@implementation Constant
+ (UIAlertView *)showAlertTipMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    return  alertView;
}



+ (void)showMessageTitle:(NSString *)title
                 message:(NSString *)message
                delegate:(id)delegate
                     tag:(int)tag
            buttonTitles:(NSString *)otherButtonTitles, ...
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil];
    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        [alertView addButtonWithTitle:arg];
    }
    va_end(args);
    [alertView show];
    alertView.tag = tag;
}
+ (void)showMessage:(NSString *)message
           delegate:(id)delegate
                tag:(int)tag
       buttonTitles:(NSString *)otherButtonTitles, ...
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil];
    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        [alertView addButtonWithTitle:arg];
    }
    va_end(args);
    [alertView show];
    alertView.tag = tag;
}


+ (void)showToastMessage:(NSString *)message
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    
    UIView *xxxx = [[UIView alloc]initWithFrame:CGRectMake(0, 64, view.bounds.size.width, view.bounds.size.height-64)];
    
    xxxx.backgroundColor = [UIColor clearColor];
    
    xxxx.tag = 120110119;
    
    [view addSubview:xxxx];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:xxxx animated:YES];
    
    // Set the annular determinate mode to show task progress.
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;    // Move to bottm center.
    hud.label.numberOfLines = 0;//可换行
    hud.offset = CGPointMake(0.f, kScreenHeight/2-100-20);
    hud.isToast = YES;
    hud.contentColor = [UIColor colorWithHexString:@"28b398"];
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.label.superview.layer.cornerRadius = 21;
    hud.label.superview.clipsToBounds = YES;
    hud.label.superview.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    hud.customView.alpha = 1;
    //    hud.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    //
    [hud hideAnimated:YES afterDelay:1.0f];
    
}
+ (void)showToastMessage:(NSString *)message AndTime:(CGFloat )time
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    
    UIView *xxxx = [[UIView alloc]initWithFrame:CGRectMake(0, 64, view.bounds.size.width, view.bounds.size.height-64)];
    
    xxxx.backgroundColor = [UIColor clearColor];
    
    xxxx.tag = 120110119;
    
    [view addSubview:xxxx];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:xxxx animated:YES];
    
    // Set the annular determinate mode to show task progress.
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;    // Move to bottm center.
    hud.label.numberOfLines = 0;//可换行
    hud.isToast = YES;

    hud.offset = CGPointMake(0.f, kScreenHeight/2-100-20);
    
    hud.contentColor = [UIColor colorWithHexString:@"28b398"];
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.label.superview.layer.cornerRadius = 21;
    hud.label.superview.clipsToBounds = YES;
    hud.label.superview.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    hud.customView.alpha = 1;
    //
    [hud hideAnimated:YES afterDelay:time];
}

+(int)netWorkingStatus
{
    //--------------------------网络状态----------------------------
    //AFN对象建立 单例
    AFNetworkReachabilityManager *NetworkingManager = [AFNetworkReachabilityManager sharedManager];
    return NetworkingManager.networkReachabilityStatus;
    /*
     AFNetworkReachabilityStatusUnknown          = -1, 未知
     AFNetworkReachabilityStatusNotReachable     = 0, 无网络连接
     AFNetworkReachabilityStatusReachableViaWWAN = 1, 局域网
     AFNetworkReachabilityStatusReachableViaWiFi = 2, wifi
     */
}

#pragma mark 获取当前屏幕显示的viewcontroller
//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
//    
//    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//    if (window.windowLevel != UIWindowLevelNormal)
//    {
//        NSArray *windows = [[UIApplication sharedApplication] windows];
//        for(UIWindow * tmpWin in windows)
//        {
//            if (tmpWin.windowLevel == UIWindowLevelNormal)
//            {
//                window = tmpWin;
//                break;
//            }
//        }
//    }
//    
//    UIView *frontView;
//    if (window.subviews.count==0) {
//        frontView = window;
//    }else
//    {
//       frontView = [[window subviews] objectAtIndex:0];
//    }
//    
//    id nextResponder = [frontView nextResponder];
//    
//    if ([nextResponder isKindOfClass:[UIViewController class]])
//    {
//        result = nextResponder;
//        if ([nextResponder isKindOfClass:[RootTabBarController class]]) {
//            AppDelegate *app = [[UIApplication sharedApplication]delegate];
//            BaseNaviController *currentNavi = app.rootTabBarController.viewControllers[app.rootTabBarController.selectedIndex];
//            result = currentNavi.viewControllers.lastObject;
//       
//        }
//    }
//    
//    else
//        result = window.rootViewController;
    
    return result;
}



#pragma mark 是否有敏感词
+(BOOL)isHaveSensitiveWord:(NSString *)originStr
{
    
//
    return NO;
}


#pragma mark 返回 documentsPath
+(NSString *)documentsPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"document地址====%@",paths[0]);
    return [paths objectAtIndex:0];
}


#pragma mark 读取工程文件
+(NSString *) ProductPath:(NSString*)filename withType:(NSString *)type{
    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:type];
    return  path;
}


#pragma mark 判断文件是否存在
+(BOOL)fileIsExists:(NSString*)fileName{
    
    
    NSString *documentPath = [Constant documentsPath];
    NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
    if([[NSFileManager defaultManager]fileExistsAtPath:filePath])
        
    {
        return true;
        
    }
    return  false;

}

#pragma mark 删除文件
+(BOOL)removeFileWithFileName:(NSString *)fileName
{
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *MapLayerDataPath = [[Constant documentsPath] stringByAppendingPathComponent:fileName];
    BOOL bRet = [fileMgr fileExistsAtPath:MapLayerDataPath];
    if (bRet) {
        //
        NSError *err;
        [fileMgr removeItemAtPath:MapLayerDataPath error:&err];
    }
    return bRet;
}

#pragma mark ----------------------------------------判断是否需要更新敏感词-------------------------
+ (void)updateSensitiveWord
{
//
}



#pragma mark md5加密
+ (NSMutableString *)encryptMd5:(NSString *)origingString
{
    //md5加密
    const char *cStr = [origingString UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
    
    //加密成md5的字符串
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}


#pragma mark 返回设备类型
+ (NSString*)deviceVersion
{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    //iPhone
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"5";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"5";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"5C";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"5C";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"5S";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"5S";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"6Plus";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"6sPlus";
    
    //iPod
    
    if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
    
    
    //iPad
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"iPad2,4"])      return @"iPad 2 (32nm)";
    if ([deviceString isEqualToString:@"iPad2,5"])      return @"iPad mini (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,6"])      return @"iPad mini (GSM)";
    if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad mini (CDMA)";
    
    if ([deviceString isEqualToString:@"iPad3,1"])      return @"iPad 3(WiFi)";
    if ([deviceString isEqualToString:@"iPad3,2"])      return @"iPad 3(CDMA)";
    if ([deviceString isEqualToString:@"iPad3,3"])      return @"iPad 3(4G)";
    if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"iPad3,5"])      return @"iPad 4 (4G)";
    if ([deviceString isEqualToString:@"iPad3,6"])      return @"iPad 4 (CDMA)";
    
    if ([deviceString isEqualToString:@"iPad4,1"])      return @"iPad Air";
    if ([deviceString isEqualToString:@"iPad4,2"])      return @"iPad Air";
    if ([deviceString isEqualToString:@"iPad4,3"])      return @"iPad Air";
    if ([deviceString isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    
    if ([deviceString isEqualToString:@"iPad4,4"]||[deviceString isEqualToString:@"iPad4,5"]||[deviceString isEqualToString:@"iPad4,6"])
        return @"iPad mini 2";
    if ([deviceString isEqualToString:@"iPad4,7"]||[deviceString isEqualToString:@"iPad4,8"]||[deviceString isEqualToString:@"iPad4,9"])  return @"iPad mini 3";
    return deviceString;
}


//#pragma mark 获取设备UUID
//+ (NSString *)getDeviceUUID
//{
//    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@" "account:@"uuid"];
//    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
//    {
//        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
//        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
//        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
//        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
//        [SSKeychain setPassword: currentDeviceUUIDStr forService:@" "account:@"uuid"];
//    }
//    return currentDeviceUUIDStr;
//}


#pragma mark 获取app的版本号
+ (NSString *)getAppVersion
{
    NSDictionary *tmpDic=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];
    
    NSString *version = [tmpDic valueForKey:@"CFBundleShortVersionString"];
    return version;
}

#pragma mark 计算文本长度
+ (float) widthForString:(NSString *)str withFontSize:(float)font
{
    float width=[str sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName]].width;
    
    return width;
}



#pragma mark 计算星座
+ (NSString *)calculateConstellationWithBirthday:(NSString *)birtyday{
    NSArray *array = [birtyday componentsSeparatedByString:@"-"];
    
    //防止没有数组数据造成崩溃
    if (array.count<2) {
        return @"";
    }
    
    NSString *monthStr = array[1];
    NSInteger month = monthStr.integerValue;
    
    NSString *dayStr = array[2];
    NSInteger day = dayStr.integerValue;
    
    
    
    NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    NSString *astroFormat = @"102123444543";
    NSString *result;
    
    if (month<1 || month>12 || day<1 || day>31){
        return @"";
    }
    
    if(month==2 && day>29)
    {
        return @"";
    }else if(month==4 || month==6 || month==9 || month==11) {
        if (day>30) {
            return @"";
        }
    }
    
    result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(month*2-(day < [[astroFormat substringWithRange:NSMakeRange((month-1), 1)] intValue] - (-19))*2,2)]];
    
    return [NSString stringWithFormat:@"%@座",result];
}



#pragma mark - 截图
+ (UIImage *)captureScrollView:(UIView *)view{
    UIImage* image = nil;
    CGSize scrollsize = CGSizeMake(view.width, view.height);
    UIGraphicsBeginImageContextWithOptions(scrollsize, YES, [UIScreen mainScreen].scale); //[UIScreen mainScreen].scal此参数为高清 可降低质量
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (image != nil) {
        return image;
    }
    return nil;
}


#pragma mark 字典转成json字符串
+ (NSString *)dictionaryToJsonString:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

@end
